package com.example.service.Impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.CostMapper;
import com.example.pojo.entity.Cost;
import com.example.service.CostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Service
@Slf4j
@DS("cost")
public class CostServiceImpl extends ServiceImpl<CostMapper, Cost> implements CostService {
    @Autowired
    private CostMapper costMapper;

    @Override
    public void add(Cost cost) {
        cost.setIsDeleted(0);
        cost.setVersion(0);
        costMapper.insert(cost);
    }

    /**
     * 分页查询
     */
//    public Page<Cost> selectPage(Integer projectId, Integer pageNum, Integer pageSize) {
//        Page<Cost> costPage = costMapper.selectPage(new Page<Cost>(pageNum, pageSize),
//                new LambdaQueryWrapper<Cost>()
//                        .like(StringUtils.hasLength(String.valueOf(projectId)), Cost::getProjectId, projectId));
//        //.like(StringUtils.hasLength(projectId), Cost::getProjectId, projectId));
//
//        return costPage;
//    }
    public Page<Cost> selectPage(String name, Integer pageNum, Integer pageSize) {
        Page<Cost> page = costMapper.selectPage(new Page<Cost>(pageNum, pageSize),
                new LambdaQueryWrapper<Cost>()
                        .like(StringUtils.hasLength(name),Cost::getName,name));
        return page;
//        QueryWrapper<Cost> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(StrUtil.isNotBlank(name),"name",cost.getName());
//        queryWrapper.eq("is_deleted",0);
//        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }
    @Override
    public void deleteById(Integer id) {
        Cost cost = costMapper.selectById(id);
        cost.setIsDeleted(1);
        updateCost(cost);
    }
    private void updateCost(Cost cost) {
        costMapper.updateById(cost);
    }

    @Override
    public Cost selectById(Integer id) {
        return costMapper.selectById(id);
    }

    @Override
    public void update(Cost cost) {
           costMapper.updateById(cost);
    }



}
